﻿using System;
using System.Linq;
using System.Collections.Generic;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.OrderDetails;

namespace Application.MainModule.OrderDetailManagement
{
    public class OrderDetailManagementService : IOrderDetailManagementService, IDisposable
    {
        #region private properties

        private IOrderDetailRepository OrderDetailRepository
        {
            get;
            set;
        }

        private IOrderDetailRepository OrderDetailTypeRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for OrderDetailService.
        /// </summary>
        /// <param name="orderDetailRepository">OrderDetailRepository dependency, usually this is resolved by IoC</param>
        public OrderDetailManagementService(IOrderDetailRepository orderDetailRepository)
        {
            if (orderDetailRepository == (IOrderDetailRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.OrderDetailRepository = orderDetailRepository;
        }

        #endregion

        #region IOrderDetailService implementations

        public void AddOrderDetail(OrderDetail orderDetail)
        {
            IUnitOfWork unitOfWork = this.OrderDetailRepository.UnitOfWork as IUnitOfWork;
            this.OrderDetailRepository.Add(orderDetail);
            unitOfWork.Commit();
        }

        public void ChangeOrderDetail(OrderDetail orderDetail)
        {
            IUnitOfWork unitOfWork = this.OrderDetailRepository.UnitOfWork as IUnitOfWork;
            this.OrderDetailRepository.Modify(orderDetail);
            unitOfWork.CommitAndRefreshChanges();
        }

        public OrderDetail FindOrderDetailById(Guid orderDetailId)
        {
            OrderDetailIdSpecification orderDetailIdSpecification = new OrderDetailIdSpecification(orderDetailId);
            return this.OrderDetailRepository.FindOrderDetail(orderDetailIdSpecification);
        }

        public List<OrderDetail> FindOrderDetailByOrderId(Guid orderId)
        {
            OrderDetailOrderIdSpecification orderDetailOrderIdSpecification = new OrderDetailOrderIdSpecification(orderId);
            return this.OrderDetailRepository.FindOrderDetails(orderDetailOrderIdSpecification).ToList<OrderDetail>();
        }

        public void RemoveOrderDetail(OrderDetail orderDetail)
        {
            if (orderDetail == (OrderDetail)null)
                throw new ArgumentNullException("orderDetail");

            IUnitOfWork unitOfWork = this.OrderDetailRepository.UnitOfWork as IUnitOfWork;
            this.OrderDetailRepository.Remove(orderDetail);
            unitOfWork.CommitAndRefreshChanges();

        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.OrderDetailRepository != null
                &&
                this.OrderDetailRepository.UnitOfWork != null)
            {
                this.OrderDetailRepository.UnitOfWork.Dispose();
            }
        }

        #endregion
    }
}
